import { isObj, isArr, isBaseType } from './index';
import deepClone from './deepClone';
/**
 *
 * @param {*} source
 * @param {*} select
 * @returns
 */

/**
 * @function zh-cn 返回所需要属性
 * @function en Return the required properties
 * @param {object} source
 * @param {string | string[]} select  'a,b,c' or '[a,b,c]'
 */
export function pick(source, select) {
  if (!isObj(source)) return source;
  const selectArr = isArr(select) ? select : select.split(',');
  const target = Object.create(null);
  const len = selectArr.length;
  for (let i = 0; i < len; i++) {
    const key = selectArr[i];
    if (source[key] !== undefined) target[key] = isBaseType(source[key]) ? source[key] : deepClone(source[key]);
  }
  return target;
}
